﻿using RXY_Mall.Api.Models;
using RXY_Mall.Core;
using RXY_Mall.Data;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;

namespace RXY_Mall.Api.Controllers
{
    /// <summary>
    /// 店铺接口
    /// </summary>
    [RoutePrefix("Shop")]
    public class ShopController : BaseApiController
    {
        private RXY_MallContext db = new RXY_MallContext();

        /// <summary>
        /// 获得店铺基本信息（第4批）
        /// </summary>
        /// <param name="shop_id">店铺ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetShop")]
        public Response<ShopOutput> GetShop(int shop_id)
        {
            Response<ShopOutput> result = new Response<ShopOutput>();
            result.code = 0;
            result.msg = "";
            try
            {
                var shop = db.gw_shops.Include(s => s.gw_shop_property).Select(s => new ShopOutput()
                {
                    shop_id = s.id,
                    shop_logo = s.logo,
                    shop_name = s.name,
                    goods_count = s.gw_shop_property.goods_count,
                    order_count = s.gw_shop_property.order_count,
                    mobile = s.mobile,
                    qq = s.qq,
                    telephone = s.telephone,
                    wechat = s.wechat,
                }).FirstOrDefault(s => s.shop_id == shop_id);
                
                if (shop == null)
                {
                    result.msg = "店铺不存在";
                    return result;
                }
                shop.shop_logo = HostHelper.GetImgUrl(shop.shop_logo);
                result.data = shop;
                result.code = 1;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
                result.code = 2;
                LogHelper.WriteLog(ex);
            }
            return result;
        }

        /// <summary>
        /// 获得所有店铺基本信息（第4批）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetShopList")]
        public async Task<Response<List<ShopOutput>>> GetShopList()
        {
            Response<List<ShopOutput>> result = new Response<List<ShopOutput>>();
            result.code = 0;
            result.msg = "";
            try
            {
                var lstshop = await db.gw_shops
                        .Include(s => s.gw_shop_property)
                        .Select(s => new ShopOutput()
                        {
                            shop_id = s.id,
                            shop_logo = s.logo,
                            shop_name = s.name
                        }).ToListAsync();

                if (lstshop == null)
                {
                    result.msg = "店铺不存在";
                    return result;
                }
                foreach (var item in lstshop)
                {
                    item.shop_logo = HostHelper.GetImgUrl(item.shop_logo);
                }
                result.data = lstshop;
                result.code = 1;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
                result.code = 2;
                LogHelper.WriteLog(ex);
            }
            return result;
        }

    }
}
